package cn.flying.base.sys.configuration.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import cn.flying.base.core.common.utils.Ft;
import cn.flying.base.core.interceptor.AbstractRequestInterceptor;

/**
 * @description: 操作日志记录
 * @author: lvyong
 * @date: 2022年11月08日 17:20
 * @version: 1.0
 */
@Component
public class LogCostInterceptor extends AbstractRequestInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        long startTime = System.currentTimeMillis();
        Ft.ThreadLocal.set("startTime", startTime);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        long startTime = (long) Ft.ThreadLocal.get("startTime");
        long endTime = System.currentTimeMillis();
        System.out.println("请求IP：" + Ft.Network.getIpAddress(request));
        System.out.println("请求资源：" + request.getRequestURI());
        System.out.println("请求开始时间：" + startTime);
        System.out.println("请求结束时间：" + endTime);
        System.out.println("本次调用时间：" + (endTime - startTime) + "毫秒");
        System.out.println("当前登录用户：" + SecurityUtils.getSubject().getPrincipal());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
